// NumStrID.h

#ifndef _NumStrID_h_
#define _NumStrID_h_

#if _MSC_VER > 1000
	#pragma once
#endif

#include "np_min_max.h"
#include "tstring.h"

/*
	NPR_ASSERT(0 == NumStrID("5").Compare(NumStrID("5")));
	NPR_ASSERT(1 == NumStrID("5").Compare(NumStrID("4")));
	NPR_ASSERT(-1 == NumStrID("4").Compare(NumStrID("5")));
	NPR_ASSERT(1 == NumStrID("11").Compare(NumStrID("1")));
	NPR_ASSERT(1 == NumStrID("11").Compare(NumStrID("2")));
	NPR_ASSERT(1 == NumStrID("11").Compare(NumStrID("9")));
	NPR_ASSERT(1 == NumStrID("11").Compare(NumStrID("10")));
	NPR_ASSERT(-1 == NumStrID("1").Compare(NumStrID("11")));
	NPR_ASSERT(-1 == NumStrID("2").Compare(NumStrID("11")));
	NPR_ASSERT(-1 == NumStrID("9").Compare(NumStrID("11")));
	NPR_ASSERT(-1 == NumStrID("10").Compare(NumStrID("11")));
	NPR_ASSERT(0 == NumStrID("0000").Compare(NumStrID("0")));
	NPR_ASSERT(0 == NumStrID("0").Compare(NumStrID("0000")));
	NPR_ASSERT(1 == NumStrID("0000").Compare(NumStrID()));
	NPR_ASSERT(-1 == NumStrID().Compare(NumStrID("0000")));
	NPR_ASSERT(0 == NumStrID().Compare(NumStrID()));
*/
struct NumStrID
{
	tstring s;

	NumStrID(){}
	explicit NumStrID(const tstring& r) : s(r) {}
	/*explicit */NumStrID(LPCTSTR r) : s(r) {}
	NumStrID(LPCTSTR str, size_t len) : s(str, len) {}

	int Compare(const NumStrID& r) const
	{
		if(this == &r)
			return 0;

		int len = max(s.length(), r.s.length());
		int ldiff = len - s.length();
		int rdiff = len - r.s.length();
		int lempty = (0==s.length() ? '0'-1 : '0');
		int rempty = (0==r.s.length() ? '0'-1 : '0');
		for(int i=0; i<len; i++)
		{
			int lvalue;
			if(i >= ldiff)
			{
				lvalue = s[i-ldiff];
			//	NPR_ASSERT('0'<=lvalue && lvalue<='9');
			}
			else
			{
				lvalue = lempty;
			}

			int rvalue;
			if(i >= rdiff)
			{
				rvalue = r.s[i-rdiff];
			//	NPR_ASSERT('0'<=rvalue && rvalue<='9');
			}
			else
			{
				rvalue = rempty;
			}

			if(lvalue < rvalue)
				return -1;
			else if(lvalue > rvalue)
				return 1;
		}
		return 0;
	}
	bool operator < (const NumStrID& r) const { return Compare(r) < 0; }
	bool operator <= (const NumStrID& r) const { return Compare(r) <= 0; }
	bool operator > (const NumStrID& r) const { return Compare(r) > 0; }
	bool operator >= (const NumStrID& r) const { return Compare(r) >= 0; }
	bool operator == (const NumStrID& r) const { return Compare(r) == 0; }
	bool operator != (const NumStrID& r) const { return Compare(r) != 0; }
};

#endif	// #ifndef _NumStrID_h_
